//班级管理store

import {
    getGrades,
    getMajors,
    getNumbers,
    queryClasses,
    deleteClasses,
    batchDeleteClasses,
    getClasses,
    addClasses,
    addStudent,
    addTeacher
} from "../../api/classes";

const state = {
    current: 0, // 存储批量导入的时候是第几步？？
    classesRecord: [] // 存储导入文件中的班级数据？？
}

const mutations = { // mutations改变state中的数据
    SET_CURRENT_ONE: (state) => { // 将current修改为1
        state.current = 1
    },
    SET_CURRENT_ZERO: (state) => { // 将current修改为0
        state.current = 0
    },
    DELETE_CLASSES: (state, classesId) => { // 删除classesId的专业元素
        for(var i = 0; i < state.classesRecord.length; i ++){
            if(state.classesRecord[i].classesId === classesId){
                state.classesRecord.splice(i, 1)
                break;
            }
        }
    },
    NULL:()=>{}
}

const actions = {
    backPre({commit}){ // 回到上一步，改变current的值
        commit('SET_CURRENT_ZERO')
    },
    classesDelete({commit}, data){ // 删除班级
        const classesId = data.classesId
        commit('DELETE_CLASSES', classesId);
    },
    getGrades(){ // 下拉获取所有年级
        return new Promise((resolve, reject) => {
            getGrades().then((data) => {
                if(data.code === 1){
                    resolve(data);
                }else{
                    reject(data)
                }
            }).catch(err=>{
                reject(err);
            })
        })
    },
    getMajors({commit},data){ // 根据年级获取专业
        const info = {
            collegeId: data.collegeId, //获取管理员ID（需要改动）
            grade: data.grade,
        };
        return new Promise((resolve, reject) => {
            getMajors(info).then((data) => {
                if(data.code === 1){
                    commit("NULL");
                    resolve(data);
                }else{
                    reject(data)
                }
            }).catch(err=>{
                reject(err);
            })
        })
    },
    getNumbers({commit},data){
        const info={
            grade: data.grade,
            major: data.major,
        };
        return new Promise((resolve, reject) => {
            getNumbers(info).then((data) => {
                if(data.code === 1){
                    commit("NULL")
                    resolve(data);
                }else{
                    reject(data)
                }
            }).catch(err=>{
                reject(err);
            })
        })
    },
    queryClasses({commit},data){
        return new Promise((resolve, reject) => {
            queryClasses(data).then(data=>{
                if(data.code===1){
                    commit("NULL");
                    resolve(data);
                }else{
                    reject(data);
                }
            }).catch(err=>{
                reject(err);
            })
        })
    },
    deleteClasses({commit},data){
        return new Promise((resolve, reject) => {
            deleteClasses(data).then(data=>{
                if(data.code===1){
                    commit("NULL");
                    resolve(data);
                }else {
                    reject(data);
                }
            }).catch(err=>{
                reject(err);
            })
        })
    },
    batchDeleteClasses({commit},data){
        return new Promise((resolve, reject) => {
            batchDeleteClasses(data).then(data=>{
                if(data.code===1){
                    commit("NULL");
                    resolve(data);
                }else {
                    reject(data);
                }
            }).catch(err=>{
                reject(err);
            })
        })
    },
    getClasses({commit},data){
        return new Promise((resolve, reject) => {
            getClasses(data).then(data=>{
                if(data.code===1){
                    commit("NULL");
                    resolve(data);
                }else{
                    reject(data)
                }
            }).catch(err=>{
                reject(err);
            })
        })
    },
    addClasses({commit},data){
        return new Promise((resolve, reject) => {
            addClasses(data).then(data=>{
                if(data.code===1){
                    commit("NULL");
                    resolve(data);
                }else{
                    reject(data)
                }
            }).catch(err=>{
                reject(err);
            })
        })
    },
    addStudent({commit},data){
        return new Promise((resolve, reject) => {
            addStudent(data).then(data=>{
                if(data.code===1){
                    commit("NULL");
                    resolve(data);
                }else{
                    reject(data)
                }
            }).catch(err=>{
                reject(err);
            })
        })
    },
    addTeacher({commit},data){
        return new Promise((resolve, reject) => {
            addTeacher(data).then(data=>{
                if(data.code===1){
                    commit("NULL");
                    resolve(data);
                }else{
                    reject(data)
                }

            }).catch(err=>{
                reject(err);
            })
        })
    },
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
